<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Wall
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: Twitter.php 2011-04-26 11:18:13 michael $
 * @author     Michael
 */

/**
 * @category   Application_Extensions
 * @package    Wall
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Wall_Plugin_Service_Twitter extends Wall_Plugin_Service_Abstract {
	
	public function check(Wall_Model_Token $token) {
		try {
			
			$setting = Engine_Api::_ ()->getDbTable ( 'settings', 'core' );
			
			$config = array ('consumerKey' => $setting->getSetting ( 'wall.service.twitter.consumerkey' ), 'consumerSecret' => $setting->getSetting ( 'wall.service.twitter.consumersecret' ) );
			
			$access = new Zend_Oauth_Token_Access ();
			$access->setParam ( Zend_Oauth_Token_Access::TOKEN_PARAM_KEY, $token->oauth_token );
			$access->setParam ( Zend_Oauth_Token_Access::TOKEN_SECRET_PARAM_KEY, $token->oauth_token_secret );
			
			$client = $access->getHttpClient ( $config );
			$client->setUri ( 'http://api.twitter.com/1/account/totals.json' );
			$client->setMethod ( Zend_Http_Client::GET );
			
			return ( bool ) $client->request ()->isSuccessful ();
		
		} catch ( Exception $e ) {
		
		}
		return false;
	}
	
	public function postAction(Wall_Model_Token $token, Activity_Model_Action $action, User_Model_User $user) {
		$host_url = Engine_Api::_ ()->wall ()->getHostUrl ();
		
		$message = html_entity_decode ( $action->body );
		//$message .= PHP_EOL . $host_url . $action->getHref();
		

		try {
			
			$setting = Engine_Api::_ ()->getDbTable ( 'settings', 'core' );
			
			$config = array ('consumerKey' => $setting->getSetting ( 'wall.service.twitter.consumerkey' ), 'consumerSecret' => $setting->getSetting ( 'wall.service.twitter.consumersecret' ) );
			
			$access = new Zend_Oauth_Token_Access ();
			$access->setParam ( Zend_Oauth_Token_Access::TOKEN_PARAM_KEY, $token->oauth_token );
			$access->setParam ( Zend_Oauth_Token_Access::TOKEN_SECRET_PARAM_KEY, $token->oauth_token_secret );
			
			$client = $access->getHttpClient ( $config );
			$client->setUri ( 'http://api.twitter.com/1/statuses/update.json' );
			$client->setMethod ( Zend_Http_Client::POST );
			$client->setParameterPost ( 'status', $message );
			
			return $client->request ()->getBody ();
		
		} catch ( Exception $e ) {
		}
		
		return false;
	
	}
	
	public function stream(Wall_Model_Token $token, $params = array()) {
		try {
			
			$setting = Engine_Api::_ ()->getDbTable ( 'settings', 'core' );
			
			$config = array ('consumerKey' => $setting->getSetting ( 'wall.service.twitter.consumerkey' ), 'consumerSecret' => $setting->getSetting ( 'wall.service.twitter.consumersecret' ) );
			
			$access = new Zend_Oauth_Token_Access ();
			$access->setParam ( Zend_Oauth_Token_Access::TOKEN_PARAM_KEY, $token->oauth_token );
			$access->setParam ( Zend_Oauth_Token_Access::TOKEN_SECRET_PARAM_KEY, $token->oauth_token_secret );
			
			$url = 'http://api.twitter.com/1/statuses/home_timeline.json';
			
			$client = $access->getHttpClient ( $config );
			$client->setUri ( $url );
			$client->setMethod ( Zend_Http_Client::GET );
			
			foreach ( $params as $key => $value ) {
				$client->setParameterGet ( $key, $value );
			}
			
			$body = $client->request ()->getBody ();
			
			if (! $body) {
				return;
			}
			$body = Zend_Json::decode ( $body );
			if (! $body) {
				return;
			}
			
			return $body;
		
		} catch ( Exception $e ) {
		}
		
		return;
	}
	
	public function postStatus(Wall_Model_Token $token, $message) {
		try {
			
			$setting = Engine_Api::_ ()->getDbTable ( 'settings', 'core' );
			
			$config = array ('consumerKey' => $setting->getSetting ( 'wall.service.twitter.consumerkey' ), 'consumerSecret' => $setting->getSetting ( 'wall.service.twitter.consumersecret' ) );
			
			$access = new Zend_Oauth_Token_Access ();
			$access->setParam ( Zend_Oauth_Token_Access::TOKEN_PARAM_KEY, $token->oauth_token );
			$access->setParam ( Zend_Oauth_Token_Access::TOKEN_SECRET_PARAM_KEY, $token->oauth_token_secret );
			
			$client = $access->getHttpClient ( $config );
			$client->setUri ( 'http://api.twitter.com/1/statuses/update.json' );
			$client->setMethod ( Zend_Http_Client::POST );
			$client->setParameterPost ( 'status', $message );
			
			return $client->request ()->getBody ();
		
		} catch ( Exception $e ) {
		}
		
		return false;
	
	}

}
 
